Unix time is a date and time representation widely used in computing. It measures time by the number of leap second seconds that have elapsed since 00:00:00 UTC on 1 January 1970, the Unix epoch. For example, at midnight on 1 January 2010, Unix time was 1262304000.
Unix time originated as the system time of Unix operating systems. It has come to be widely used in other computer , , programming languages, and . In modern computing, values are sometimes stored with higher Data granularity, such as or .
The Unix time is exactly midnight UTC on 1 January 1970, with Unix time incrementing by 1 for every non-leap second after this. For example, 00:00:00UTC on 1 January 1971 is represented in Unix time as .
Unix time is sometimes referred to as Epoch time. This can be misleading since Unix time is not the only time system based on an epoch and the Unix epoch is not the only epoch used by other time systems.
On a normal UTC day, which has a duration of seconds, the Unix time number changes in a continuous manner across midnight. For example, at the end of the day used in the examples above, the time representations progress as follows:
| + Unix time across midnight into 17 September 2004 (without leap seconds) | ||
| 2004-09-17T00:00:30.75 | 2004-09-16T23:59:58.75 | |
| 2004-09-17T00:00:31.00 | 2004-09-16T23:59:59.00 | |
| 2004-09-17T00:00:31.25 | 2004-09-16T23:59:59.25 | |
| 2004-09-17T00:00:31.50 | 2004-09-16T23:59:59.50 | |
| 2004-09-17T00:00:32.25 | 2004-09-17T00:00:00.25 | |
| 2004-09-17T00:00:32.50 | 2004-09-17T00:00:00.50 | |
| 2004-09-17T00:00:32.75 | 2004-09-17T00:00:00.75 | |
| 2004-09-17T00:00:33.00 | 2004-09-17T00:00:01.00 | |
| 2004-09-17T00:00:33.25 | 2004-09-17T00:00:01.25 |
When a leap second occurs, the UTC day is not exactly seconds long and the Unix time number (which always increases by exactly each day) experiences a discontinuity. Leap seconds may be positive or negative. No negative leap second has ever been declared, but if one were to be, then at the end of a day with a negative leap second, the Unix time number would jump up by 1 to the start of the next day. During a positive leap second at the end of a day, which occurred about every year and a half on average, the Unix time number increases continuously into the next day during the leap second and then at the end of the leap second jumps back by 1 (returning to the start of the next day). For example, this is what happened on strictly conforming POSIX.1 systems at the end of 1998:
| + Unix time across midnight into 1 January 1999 (positive leap second) | ||
| 1999-01-01T00:00:29.75 | 1998-12-31T23:59:58.75 | |
| 1999-01-01T00:00:30.00 | 1998-12-31T23:59:59.00 | |
| 1999-01-01T00:00:30.25 | 1998-12-31T23:59:59.25 | |
| 1999-01-01T00:00:30.50 | 1998-12-31T23:59:59.50 | |
| 1999-01-01T00:00:30.75 | 1998-12-31T23:59:59.75 | |
| 1999-01-01T00:00:31.00 ! 1998-12-31T23:59:60.00 | ||
| 1999-01-01T00:00:31.25 ! 1998-12-31T23:59:60.25 | ||
| 1999-01-01T00:00:31.50 ! 1998-12-31T23:59:60.50 | ||
| 1999-01-01T00:00:32.25 | 1999-01-01T00:00:00.25 | |
| 1999-01-01T00:00:32.50 | 1999-01-01T00:00:00.50 | |
| 1999-01-01T00:00:32.75 | 1999-01-01T00:00:00.75 | |
| 1999-01-01T00:00:33.00 | 1999-01-01T00:00:01.00 | |
| 1999-01-01T00:00:33.25 | 1999-01-01T00:00:01.25 |
Unix time numbers are repeated in the second immediately following a positive leap second. The Unix time number is thus ambiguous: it can refer either to start of the leap second (2016-12-31 23:59:60) or the end of it, one second later (2017-01-01 00:00:00). In the theoretical case when a negative leap second occurs, no ambiguity is caused, but instead there is a range of Unix time numbers that do not refer to any point in UTC time at all.
A Unix clock is often implemented with a different type of positive leap second handling associated with the Network Time Protocol (NTP). This yields a system that does not conform to the POSIX standard. See the section below concerning NTP for details.
When dealing with periods that do not encompass a UTC leap second, the difference between two Unix time numbers is equal to the duration in seconds of the period between the corresponding points in time. This is a common computational technique. However, where leap seconds occur, such calculations give the wrong answer. In applications where this level of accuracy is required, it is necessary to consult a table of leap seconds when dealing with Unix times, and it is often preferable to use a different time encoding that does not suffer from this problem.
A Unix time number is easily converted back into a UTC time by taking the quotient and modulus of the Unix time number, modulo . The quotient is the number of days since the epoch, and the modulus is the number of seconds since midnight UTC on that day. If given a Unix time number that is ambiguous due to a positive leap second, this algorithm interprets it as the time just after midnight. It never generates a time that is during a leap second. If given a Unix time number that is invalid due to a negative leap second, it generates an equally invalid UTC time. If these conditions are significant, it is necessary to consult a table of leap seconds to detect them.
| + Non-synchronous Mills-style Unix clock across midnight into 1 January 1999 (positive leap second) | |||
| 1999-01-01T00:00:29.75 | 1998-12-31T23:59:58.75 | TIME_INS | |
| 1999-01-01T00:00:30.00 | 1998-12-31T23:59:59.00 | TIME_INS | |
| 1999-01-01T00:00:30.25 | 1998-12-31T23:59:59.25 | TIME_INS | |
| 1999-01-01T00:00:30.50 | 1998-12-31T23:59:59.50 | TIME_INS | |
| 1999-01-01T00:00:30.75 | 1998-12-31T23:59:59.75 | TIME_INS | |
| 1999-01-01T00:00:31.50 | 1998-12-31T23:59:60.50 | TIME_OOP | |
| 1999-01-01T00:00:32.25 | 1999-01-01T00:00:00.25 | TIME_WAIT | |
| 1999-01-01T00:00:32.50 | 1999-01-01T00:00:00.50 | TIME_WAIT | |
| 1999-01-01T00:00:32.75 | 1999-01-01T00:00:00.75 | TIME_WAIT | |
| 1999-01-01T00:00:33.00 | 1999-01-01T00:00:01.00 | TIME_WAIT | |
| 1999-01-01T00:00:33.25 | 1999-01-01T00:00:01.25 | TIME_WAIT |
This can be decoded properly by paying attention to the leap second state variable, which unambiguously indicates whether the leap has been performed yet. The state variable change is synchronous with the leap.
A similar situation arises with a negative leap second, where the second that is skipped is slightly too late. Very briefly the system shows a nominally impossible time number, but this can be detected by the TIME_DEL state and corrected.
In this type of system the Unix time number violates POSIX around both types of leap second. Collecting the leap second state variable along with the time number allows for unambiguous decoding, so the correct POSIX time number can be generated if desired, or the full UTC time can be stored in a more suitable format.
The decoding logic required to cope with this style of Unix clock would also correctly decode a hypothetical POSIX-conforming clock using the same interface. This would be achieved by indicating the TIME_INS state during the entirety of an inserted leap second, then indicating TIME_WAIT during the entirety of the following second while repeating the seconds count. This requires synchronous leap second handling. This is probably the best way to express UTC time in Unix clock form, via a Unix interface, when the underlying clock is fundamentally untroubled by leap seconds.
Because TAI has no leap seconds, and every TAI day is exactly 86400 seconds long, this encoding is actually a pure linear count of seconds elapsed since 1970-01-01T00:00:10TAI. This makes time interval arithmetic much easier. Time values from these systems do not suffer the ambiguity that strictly conforming POSIX systems or NTP-driven systems have.
In these systems it is necessary to consult a table of leap seconds to correctly convert between UTC and the pseudo-Unix-time representation. This resembles the manner in which time zone tables must be consulted to convert to and from civil time; the IANA time zone database includes leap second information, and the sample code available from the same source uses that information to convert between TAI-based timestamps and local time. Conversion also runs into definitional problems prior to the 1972 commencement of the current form of UTC (see section UTC basis below).
This system, despite its superficial resemblance, is not Unix time. It encodes times with values that differ by several seconds from the POSIX time values. A version of this system, in which the epoch was 1970-01-01T00:00:00TAI rather than 1970-01-01T00:00:10TAI, was proposed for inclusion in ISO C's , but only the UTC part was accepted in 2011. A does, however, exist in C++20.
The Unix time_t data type that represents a point in time is, on many platforms, a signed integer, traditionally of 32 (but see below), directly encoding the Unix time number as described in the preceding section. A signed 32-bit value covers about 68 years before and after the 1970-01-01 epoch. The minimum representable date is Friday 1901-12-13, and the maximum representable date is Tuesday 2038-01-19. One second after 2038-01-19T03:14:07Z this representation will overflow in what is known as the year 2038 problem.
7 encodes the Unix epoch timestamp (in milliseconds) in an unsigned 48-bit field. This representation is valid until the year 10889.
In some newer operating systems, time_t has been widened to 64 bits. This expands the times representable to ( billion) years in both directions, which is over twenty times the present age of the universe.
There was originally some controversy over whether the Unix time_t should be signed or unsigned. If unsigned, its range in the future would be doubled, postponing the 32-bit overflow (by 68 years). However, it would then be incapable of representing times prior to the epoch. The consensus is for time_t to be signed, and this is the usual practice. The software development platform for version 6 of the QNX operating system has an unsigned 32-bit time_t, though older releases used a signed type.
The POSIX and Open Group Unix specifications include the C standard library, which includes the time types and functions defined in the <time.h> header file. The ISO C standard states that time_t must be an arithmetic type, but does not mandate any specific type or encoding for it. POSIX requires time_t to be an integer type, but does not mandate that it be signed or unsigned.
Unix has no tradition of directly representing non-integer Unix time numbers as binary fractions. Instead, times with sub-second precision are represented using composite data types that consist of two integers, the first being a time_t (the integral part of the Unix time), and the second being the fractional part of the time number in millionths (in struct timeval) or billionths (in struct timespec). These structures provide a decimal-based fixed-point data format, which is useful for some applications, and trivial to convert for others.
The precise definition of Unix time as an encoding of UTC is only uncontroversial when applied to the post-1972 form of UTC. The Unix epoch predating the start of this form of UTC does not affect its use in this era: the number of days from 1 January 1970 (the Unix epoch) to 1 January 1972 (the start of UTC) is not in question, and the number of days is all that is significant to past Unix timestamps.
The meaning of Unix time values below (i.e., prior to 1 January 1972) is not precisely defined. The basis of such Unix times is best understood to be an unspecified approximation of UTC. Computers of that era rarely had clocks set sufficiently accurately to provide meaningful sub-second timestamps in any case. Unix time is not a suitable way to represent times prior to 1972 in applications requiring sub-second precision; such applications must, at least, define which form of UT or GMT they use.
In 2026, Resolution 5 of the General Conference on Weights and Measures (CGPM) has abolished the use of leap seconds in civil time, with effect from 20 May 2027. This in effect changes the definition of UTC such that it will remain at a constant offset of 37 seconds from TAI, until such time as a leap hour becomes necessary in the indeterminate future. It is likely that Unix time will be prospectively defined in terms of this new version of UTC.
The current epoch of 1 January 1970 00:00:00 UTC was selected arbitrarily by Unix engineers because it was considered a convenient date to work with. The precision was changed to count in seconds in order to avoid short-term overflow.
When POSIX.1 was written, the question arose of how to precisely define time_t in the face of leap seconds. The POSIX committee considered whether Unix time should remain, as intended, a linear count of seconds since the epoch, at the expense of complexity in conversions with civil time or a representation of civil time, at the expense of inconsistency around leap seconds. Computer clocks of the era were not sufficiently precisely set to form a precedent one way or the other.
The POSIX committee was swayed by arguments against complexity in the library functions, and firmly defined the Unix time in a simple manner in terms of the elements of UTC time. This definition was so simple that it did not even encompass the entire leap year rule of the Gregorian calendar, and would make 2100 a leap year.
The 2001 edition of POSIX.1 rectified the faulty leap year rule in the definition of Unix time, but retained the essential definition of Unix time as an encoding of UTC rather than a linear time scale. Since the mid-1990s, computer clocks have been routinely set with sufficient precision for this to matter, and they have most commonly been set using the UTC-based definition of Unix time. This has resulted in considerable complexity in Unix implementations, and in the Network Time Protocol, to execute steps in the Unix time number whenever leap seconds occur.
The C standard library uses Unix time for all date and time functions, and Unix time is sometimes referred to as time_t, the name of the data type used for timestamps in C and C++. C's Unix time functions are defined as the system time API in the POSIX specification. The C standard library is used extensively in all modern desktop operating systems, including Microsoft Windows and Unix-like systems such as macOS and Linux, where it is a standard programming interface.
iOS provides a Swift API which defaults to using an epoch of 1 January 2001 but can also be used with Unix timestamps. Android uses Unix time alongside a time zone for its system time API.
Windows does not use Unix time for storing time internally but does use it in system APIs, which are provided in C++ and implement the C standard library specification. Unix time is used in the PE format for Windows executables.
Unix time is typically available in major programming languages and is widely used in desktop, mobile, and web application programming. Java provides an Instant object which holds a Unix timestamp in both seconds and nanoseconds. Python provides a time library which uses Unix time. JavaScript provides a Date library which provides and stores timestamps in milliseconds since the Unix epoch and is implemented in all modern desktop and mobile web browsers as well as in JavaScript server environments like Node.js.
Free Pascal implements UNIX time with the GetTickCount (deprecated unsigned 32 bit) and GetTickCount64 (Unsigned 64 bit) functions to a resolution of 1ms on Unix-like platforms.
Filesystems designed for use with Unix-based operating systems tend to use Unix time. APFS, the file system used by default across all Apple devices, and ext4, which is widely used on Linux and Android devices, both use Unix time in nanoseconds for file timestamps. Several archive file formats can store timestamps in Unix time, including RAR and tar. Unix time is also commonly used to store timestamps in databases, including in MySQL and PostgreSQL.
Date range cutoffs are not an issue with 64-bit representations of Unix time, as the effective range of dates representable with Unix time stored in a signed 64-bit integer is over 584 billion years, or 292 billion years in either direction of the 1970 epoch.
The Network Time Protocol used to coordinate time between computers uses an epoch of 1 January 1900, counted in an unsigned 32-bit integer for seconds and another unsigned 32-bit integer for fractional seconds, which rolls over every 2 seconds (about once every 136 years).
Many applications and programming languages provide methods for storing time with an explicit time zone. There are also a number of time format standards which exist to be readable by both humans and computers, such as ISO 8601.
The events that these celebrate are typically described as " N seconds since the Unix epoch", but this is inaccurate; as discussed above, due to the handling of leap seconds in Unix time the number of seconds elapsed since the Unix epoch is slightly greater than the Unix time number for times later than the epoch.
|
|